package com.zxx.pattern.mementoPattern;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zxx.designpattern.R;

public class MainMementoActivity extends AppCompatActivity {

    private NoteEditText mNoteEditText;
    private TextView mSaveTv;
    private ImageView mUndoBtn;
    private ImageView mRedoBtn;

    //备忘录管理器
    private NoteCaretaker mCaretaker = new NoteCaretaker();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_memento);

        initViews();
    }

    private void initViews() {
        mNoteEditText = findViewById(R.id.note_edittext);
        mUndoBtn = findViewById(R.id.undo_btn);
        mRedoBtn = findViewById(R.id.redo_btn);
        mSaveTv = findViewById(R.id.save_btn);

        mUndoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回上一个节点
                mNoteEditText.restore(mCaretaker.getPrevMemoto());

                makeToast("撤销：");
            }
        });

        mRedoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //恢复下一个记录点
                mNoteEditText.restore(mCaretaker.getNextMemoto());

                makeToast("重做：");
            }
        });

        mSaveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCaretaker.saveMemoto(mNoteEditText.createMemoto());

                makeToast("保存笔记 : ");
            }
        });



    }

    private void makeToast(String msg) {
        Toast.makeText(this, msg + mNoteEditText.getText() + ", 光标位置：" + mNoteEditText.getSelectionStart(), Toast.LENGTH_SHORT).show();
    }
}